package me.study.concurrent;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

/**
 *	延迟队列 DelayQueue
 *  @author GJ-Alan
 */
public class DelayBlockingTest {

	/**
	 * 测试callable接口是否可以单独使用
	 *
	 * @author GJ-Alan
	 * @throws Exception
	 * @date 2017年3月16日
	 */
	@Test
	public void test() throws Exception {
		String call = new Callable<String>() {

			public String call() throws Exception {
				System.out.println("可以独立运行");
				return "yes";
			}
			
		}.call();
		System.out.println("返回值是："+call);
	}
	
	
	/**
	 * 测试延迟阻塞队列DelayQueue
	 *
	 * @author GJ-Alan
	 * @throws InterruptedException 
	 * @date 2017年3月16日
	 */
	@Test
	public void test2() throws InterruptedException {
		class DelayTest implements  Delayed{
			//私有属性message
			private String message;
			public String getMessage() {
				return message;
			}
			public void setMessage(String message) {
				this.message = message;
			}
			public int compareTo(Delayed o) {
				return 0;
			}
			public long getDelay(TimeUnit unit) {
				
				return 0;
			}}
		
		DelayQueue<DelayTest> delayQueue=new DelayQueue<DelayTest>();
		DelayTest test=new DelayTest();
		test.setMessage("delay测试");
		test.getDelay(TimeUnit.MILLISECONDS);
		delayQueue.put(test);
		System.out.println("getDelay返回："+test.getDelay(TimeUnit.SECONDS));
		//取出元素
		System.out.println("从delay队列取出的元素是："+delayQueue.take().getMessage());
		System.out.println("从delay队列取出的元素是："+delayQueue.take().getMessage());
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}
